package net.maku.generator.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import net.maku.generator.domain.query.Query;
import net.maku.generator.dao.TemplateDao;
import net.maku.generator.domain.entity.TemplateEntity;
import net.maku.generator.service.TemplateService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

@Service
public class TemplateServiceImpl implements TemplateService {

    @Autowired
    private TemplateDao templateDao;

    @Override
    public void add(TemplateEntity template) {
        templateDao.insert(template);
    }

    @Override
    public void update(TemplateEntity template) {
        templateDao.updateById(template);
    }

    @Override
    public void deleteBatchIds(List<Long> idList) {
        templateDao.deleteBatchIds(idList);
    }

    @Override
    public TemplateEntity getById(Long id) {
        return templateDao.selectById(id);
    }

    @Override
    public void deleteByGroupIds(List<Long> idList) {
        templateDao.delete(Wrappers.lambdaQuery(TemplateEntity.class).in(TemplateEntity::getTemplateGroupId, idList));
    }

    @Override
    public List<TemplateEntity> list(Query query) {
        LambdaQueryWrapper<TemplateEntity> queryWrapper = Wrappers.lambdaQuery(TemplateEntity.class);
        queryWrapper.eq(query.getTemplateGroupId() != null, TemplateEntity::getTemplateGroupId, query.getTemplateGroupId());
        queryWrapper.like(StrUtil.isNotBlank(query.getTemplateName()), TemplateEntity::getTemplateName, query.getTemplateName());
        return templateDao.selectList(queryWrapper);
    }

}